package service

import (
	"fmt"
	mls "github.com/PhlexTheCharr/gofurry-cloud-service/apps/openlist/manage/log/service"
	"github.com/PhlexTheCharr/gofurry-cloud-service/apps/openlist/manage/util/dao"
	"github.com/PhlexTheCharr/gofurry-cloud-service/apps/openlist/manage/util/models"
	"github.com/PhlexTheCharr/gofurry-cloud-service/common"
	ca "github.com/PhlexTheCharr/gofurry-cloud-service/common/abstract"
	"github.com/PhlexTheCharr/gofurry-cloud-service/common/log"
	cm "github.com/PhlexTheCharr/gofurry-cloud-service/common/models"
	"github.com/PhlexTheCharr/gofurry-cloud-service/common/util"
	"time"
)

type utilManageService struct{}

var utilManageSingleton = new(utilManageService)

func GetUtilManageService() *utilManageService { return utilManageSingleton }

// 新增金句
func (svc utilManageService) AddSaying(req models.AddSayingRequest) common.GFError {
	// 检验入参合法性
	errorResults := ca.ValidateServiceApi.Validate(req)
	if errorResults != nil {
		return common.NewServiceError("传入参数有误")
	}

	var newSaying = &models.OlSaying{
		ID:     util.GenerateId(),
		Author: req.Author,
		Saying: req.Saying,
	}
	newSaying.CreateTime = cm.LocalTime(time.Now())
	newSaying.UpdateTime = newSaying.CreateTime

	// 记录入库
	err := dao.GetUtilManageDao().Add(newSaying)
	if err != nil {
		return common.NewServiceError(err.GetMsg())
	}

	info := fmt.Sprintf("ol_saying新增记录, 内容 = %v", newSaying.Saying)
	mls.GetManageLogService().AddManageLog("1", info)

	return nil
}

// 分页查询金句
func (svc utilManageService) Page(req *models.SayingPageQuery) (sayingPage cm.PageResponse, err common.GFError) {
	res, dbErr := dao.GetUtilManageDao().Paginate(req)
	if dbErr != nil {
		return sayingPage, common.NewServiceError("分页查询失败.")
	}
	return res, nil
}

// 批量删除金句
func (svc utilManageService) SayingDeleteList(req models.SayingDeleteRequest) common.GFError {
	if req.ID == nil {
		return common.NewServiceError("传入ID为空.")
	}

	for idx := range req.ID {
		tagDelete := svc.SayingDelete(req.ID[idx])
		if tagDelete != nil {
			log.Error(tagDelete)
		}
	}
	return nil
}

// 删除单个金句
func (svc utilManageService) SayingDelete(id int64) common.GFError {
	var record *models.OlSaying
	_ = dao.GetUtilManageDao().GetById(id, &record)

	var idList []int64
	idList = append(idList, id)
	_, err := dao.GetUtilManageDao().Delete(idList, models.OlSaying{})
	if err == nil {
		info := fmt.Sprintf("ol_saying删除记录, 作者 = %v, 内容 = %v", record.Author, record.Saying)
		mls.GetManageLogService().AddManageLog("3", info)
	}
	return err
}

// 修改金句
func (svc utilManageService) UpdateSaying(req models.UpdateSayingRequest) common.GFError {
	// 检验入参合法性
	errorResults := ca.ValidateServiceApi.Validate(req)
	if errorResults != nil {
		return common.NewServiceError("传入参数有误")
	}

	var newSaying = &models.OlSaying{
		ID:     req.ID,
		Author: req.Author,
		Saying: req.Saying,
	}
	newSaying.UpdateTime = cm.LocalTime(time.Now())

	// 记录入库
	_, err := dao.GetUtilManageDao().Update(req.ID, newSaying)
	if err != nil {
		return common.NewServiceError(err.GetMsg())
	}

	info := fmt.Sprintf("ol_saying修改记录, 新内容 = %v", newSaying.Saying)
	mls.GetManageLogService().AddManageLog("1", info)

	return nil
}
